﻿using MediatR;
using Medical.Api.Write.Application.Command.RecyclingCleaning;
using Medical.Domain.RecoveryCleaning;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Medical.Api.Write.Application.Handler.RecyclingCleaning
{
    /// <summary>
    /// 逻辑删除登记清洗信息命令处理
    /// </summary>
    public class IsDelRegistrationCleaningCommandHandlter : IRequestHandler<IsDelRegistrationCleaningCommand, ApiResult<int>>
    {
        /// <summary>
        /// 登记清洗仓储
        /// </summary>
        private readonly IBaseRepository<CleaningRegistration> _repository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<IsDelRegistrationCleaningCommandHandlter> _logger;

        public IsDelRegistrationCleaningCommandHandlter(
            IBaseRepository<CleaningRegistration> repository,
            ILogger<IsDelRegistrationCleaningCommandHandlter> logger)
        {
            _repository = repository;
            _logger = logger;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>逻辑删除登记清洗信息</returns>

        public async Task<ApiResult<int>> Handle(IsDelRegistrationCleaningCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 查询指定ID的清洗登记信息
                var entity =  _repository.GetAll().FirstOrDefault(x=>x.Id==request.Id);
                
                // 检查实体是否存在
                if (entity == null)
                {
                    _logger.LogWarning($"未找到ID为{request.Id}的清洗登记信息");
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Msg = "未找到指定的清洗登记信息",
                        Data = 0
                    };
                }

                // 执行逻辑删除
                entity.IsDeleted = true;
                entity.UpdateTime = DateTime.Now;
                
                // 更新实体
                var result = _repository.Update(entity);

                if (result > 0)
                {
                    _logger.LogInformation($"成功逻辑删除ID为{request.Id}的清洗登记信息");
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Msg = "删除成功",
                        Data = result
                    };
                }
                else
                {
                    _logger.LogWarning($"逻辑删除ID为{request.Id}的清洗登记信息失败");
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Msg = "删除失败",
                        Data = 0
                    };
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"逻辑删除清洗登记信息时发生异常: {ex.Message}");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Error,
                    Msg = $"系统错误: {ex.Message}",
                    Data = 0
                };
            }
        }
    }
}
